/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.view;

import java.util.Locale;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;

/**
 *
 * @author Walter Lütgenau
 */
public class BaseApplet extends JApplet {
    public static final String SHOW_SAME_WINDOW = "_self";
    public static final String SHOW_NEW_TAB = "_blank";
    public static final String PAR_LOCALE = "locale";
    public static final String PAR_CONFIG_FILES = "configFiles";
    public static final String SEPARATOR_LOCALE = "_";
    public static final String SEPARATOR_CONFIG_FILES = ",";

    /**
     * Make sure the init will be done on the event dispatching thread.
     */
    @Override
    public void init() {
        if (SwingUtilities.isEventDispatchThread()) {
            initialize();
            return;
        }

        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    initialize();
                }
            });
            super.init();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    protected void initialize() {
        try {
            NodesLookAndFeelManager.setLookAndFeel();
            SwingUtilities.updateComponentTreeUI( this );
            setLanguage();
            initUI();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * Make sure the start will be done on the event dispatching thread.
     */
    @Override
    public void start() {
        if (SwingUtilities.isEventDispatchThread()) {
            startUI();
            return;
        }

        try {
            SwingUtilities.invokeAndWait(new Runnable() {

                @Override
                public void run() {
                    startUI();
                }
            });
            super.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void stop() {
    }

    public void destroy() {
    }

    /**
     * If the user language has been specified as a parameter, use it
     */
    protected void setLanguage() {
        String locale = getParameter(PAR_LOCALE);
        if (locale == null)
            return;

        String language = "";
        String country = "";

        if (locale.indexOf(SEPARATOR_LOCALE) >= 0) {
            String[] locales = locale.split(SEPARATOR_LOCALE);
            if (locales.length == 2) {
                language = locales[0];
                country = locales[1];
            }
        }

        try {
            Locale.setDefault(new Locale(language, country));
        } catch(SecurityException se) {
            // do nothing.
        }
    }

    /**
     * Empty implementation, if init is not used. Else override the implementation.
     */
    protected void initUI() {
    }

    /**
     * Empty implementation, if start is not used. Else override the implementation
     */
    protected void startUI() {
    }

}
